home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 June / EnigmA AMIGA RUN 08 (1996)(G.R. Edizioni)(IT)[!][issue 1996-06][EARSAN CD VII].iso / earcd / comm1 / npns.lha / npns / postnews.c < prev    next >
C/C++ Source or Header  |  1996-04-20  |  11KB  |  455 lines

  1.  
  2. /*
  3.  *    Program     postnews
  4.  *    Programmer    N.d'Alterio
  5.  *    Date        06/07/95
  6.  *
  7.  *  Synopsis:    This program spools an news article ready for posting at
  8.  *              some later time. If currently online then sendnews is 
  9.  *              called and the article will be sent off immediately.
  10.  *        The online by checking env var ONLINE
  11.  *
  12.  *        This is a heavily modified version of postnews by 
  13.  *              James Burton from his postnewsspool package. This fixes
  14.  *              a number of bugs such as the leaving of locks, hanging
  15.  *              for a long time while waiting to see if it is online.
  16.  *        In addition the program has been very much streamlined
  17.  *              there are no longer the command line options -X, -R as
  18.  *              these seemd to have no use. Also gone are the arg### and
  19.  *              ref### files it produced which seemed to be useless.
  20.  *    
  21.  *        For tin users the program has the command line option
  22.  *              -f the add a From: user@somewhere to the header.
  23.  *
  24.  *        This program has the paths for my setup compiled into
  25.  *              it as defaults to use different paths and files there
  26.  *              is an env variable PNS_SPOOLDIR which is the full directory
  27.  *              name for the spool dir with no trailing '/'.
  28.  *
  29.  *              The program expects to have the companion program sendnews
  30.  *              in AmiTCP:bin/sendnews which in turn needs to have the 
  31.  *              NNTP posting program as AmiTCP:bin/NNTPPost.     
  32.  *
  33.  *  Command Line Arguments:    -n        Does not send if online
  34.  *                -R        Specifies header filename
  35.  *                -x        File to delete if successful
  36.  *                -f        Adds a From: someone@ line to header
  37.  *                              -h              Prints some help info
  38.  *                filename    The article file to post
  39.  *                                              if omitted will take from STDIN
  40.  *
  41.  *  Inputs:        article            Should contain a full header
  42.  *            or
  43.  *            header            article header
  44.  *            body            article body
  45.  *
  46.  *  Outputs:        spooldir/msg.###     The spooled message
  47.  *
  48.  *  Variables:        int i            general loop var
  49.  *            int exit_code        return code for prog
  50.  *            BPTR lock        lock on spooldir
  51.  *            char *Buffer         string to build filenames
  52.  *            FILE *msg_fptr        file to contain spooled  article
  53.  *            FILE *body_fptr        file containing article body
  54.  *            FILE *head_fptr        fiel containing article header
  55.  *            int no_post        flag - don't try to send
  56.  *            char *from        Pointer to from string
  57.  *            char *body_fname    Pointer to body filename 
  58.  *            char *head_fname    Pointer to header filename
  59.  *            int status        delete status flag
  60.  *            int num_del         Number of files to be deleted
  61.  *            char *Todelete[]    Buffer containing names of files to delete
  62.  *
  63.  *  Functions:        copyfile        copies article to spooldir
  64.  *            NextArg            checks for existance of next argument
  65.  *
  66.  * $VER: postnews.c 2.1 (07.08.95) $        
  67.  * $Log: postnews.c $
  68.  * Revision 4.2  1996/04/20  13:31:31  nagd
  69.  * new online check via ONLINE env var
  70.  * remove DOS library open/close code
  71.  * removed arg -a
  72.  * sendnews now only needs to be on path
  73.  *
  74.  * Revision 4.1  1995/12/19  17:47:57  nagd
  75.  * addded options -R, -x, -a new online check
  76.  *
  77.  *
  78.  * Revision 3.3  1995/09/30  21:25:18  daltern
  79.  * new command line option -n
  80.  *
  81.  * Revision 3.2  1995/09/30  21:17:20  daltern
  82.  * Added new env var PNS_OFFLINE
  83.  * improved error handling
  84.  *
  85.  * Revision 3.1  1995/09/10  16:41:41  daltern
  86.  * Removed need for seq file to comply with v3.1 of sendnews
  87.  * spooled messages now have name of form msg.##########
  88.  * where ###### is the long date returned by time()
  89.  *
  90.  *
  91.  * Revision 1.2  1995/08/27  21:49:10  daltern
  92.  * Added env vars for path options
  93.  * fixed a bug where a file was left open
  94.  *
  95.  * Revision 1.1  1995/08/07  14:17:55  daltern
  96.  * Initial revision
  97.  *
  98.  *
  99.  */
  100.  
  101.  
  102. #include <stdio.h>                   
  103. #include <stdlib.h>
  104. #include <string.h>
  105. #include <time.h>
  106. #include <dos/dos.h>
  107. #include <clib/dos_protos.h>
  108.  
  109. #define SPOOLDIR    "AmiTCP:Usr/Spool/News"
  110. #define MAX_DEL        16
  111.  
  112. void  copyfile( FILE *, FILE * );
  113. int   NextArg( int, int, char ** );
  114.  
  115. extern char *version = "$VER: postnews 4.2 (20.04.95) Nick d'Alterio";
  116.  
  117. int main( int argc,char **argv )
  118.  
  119. {
  120.  
  121.   FILE *msg_fptr;
  122.   FILE *head_fptr;
  123.   FILE *body_fptr;
  124.     
  125.   int i;          
  126.   int exit_code = 0;
  127.   int no_post   = FALSE;
  128.   int num_del;
  129.   int con_status;
  130.   int status;
  131.  
  132.   char *spooldir;
  133.   char *from;
  134.   char *body_fname;
  135.   char *head_fname;
  136.   char *online;
  137.  
  138.   char Buffer[200];
  139.   
  140.   char *Todelete[MAX_DEL];   
  141.  
  142.   BPTR lock;                     
  143.  
  144. /*
  145.  *   Read spool dir from env variable.
  146.  */
  147.  
  148.   if ( ( spooldir = getenv( "PNS_SPOOLDIR" ) ) == NULL ) {
  149.     spooldir = SPOOLDIR;
  150.   }   /* end if */
  151.  
  152. /*
  153.  *   See if user wants to post or not if online.
  154.  */
  155.  
  156.   if ( getenv( "PNS_OFFLINE" ) ) {
  157.     no_post = TRUE;
  158.   }   /* end if */
  159.  
  160.  
  161. /*
  162.  *=================================================================================================*/
  163.  
  164. /*
  165.  *   Parse command line.
  166.  */
  167.   
  168.   i          = 1;
  169.   num_del    = 0;
  170.   from       = NULL;
  171.   head_fname = NULL;
  172.   body_fname = NULL;
  173.     
  174.   while ( i < argc ) {    
  175.  
  176.     if ( argv[i][0] == '-' ) {   
  177.  
  178.     switch ( argv[i][1] ) {
  179.  
  180. /*
  181.  *  Add a From: line to article.
  182.  */
  183.  
  184.                 case 'f' : 
  185.  
  186.             if ( NextArg( i, argc, argv ) ) {
  187.  
  188.                 from = argv[i+1];
  189.                                 
  190.             } else {
  191.  
  192.                 exit(10);
  193.  
  194.             }  /* end if */
  195.             i = i + 2;
  196.             break;
  197.  
  198. /*
  199.  *   Dont attempt to post if online.
  200.  */
  201.  
  202.         case 'n' :
  203.  
  204.             no_post = TRUE;
  205.             i++;
  206.             break;
  207.  
  208. /*
  209.  *   Specifies a header for the article.
  210.  */
  211.  
  212.         case 'R' :
  213.  
  214.             if ( NextArg( i, argc, argv ) ) {
  215.                 head_fname = argv[i+1];                        
  216.             } else {
  217.                 exit(10);
  218.             }  /* end if */
  219.             i = i + 2;
  220.             break;
  221.  
  222. /*
  223.  *   Set up a buffer of filenames to delete if 
  224.  *   successful in posting.
  225.  */
  226.                             
  227.         case 'x' :
  228.  
  229.             if ( NextArg( i, argc, argv ) ) {
  230.                 if ( num_del < MAX_DEL ) {
  231.                     Todelete[num_del] = argv[i+1];
  232.                     num_del++;
  233.                 } else {
  234.                     fprintf( stderr, " Option '-x' used to many times ignoring\n" );
  235.                 }   /* end if */
  236.             } else {
  237.                 exit(10);
  238.             }   /* end if */
  239.             i = i + 2;
  240.             break;
  241.  
  242. /*
  243.  *   Print help.
  244.  */
  245.  
  246.         case 'h' :
  247.  
  248.             fprintf( stderr, " Usage: %s article [options] or %s [options] < article\n", argv[0], argv[0] );
  249.                        fprintf( stderr, " Where options are :-\n" );
  250.             fprintf( stderr, "\t-f address\tAdd a From: address line to article\n" );
  251.             fprintf( stderr, "\t-n        \tDon't try to send article if online\n" );
  252.             fprintf( stderr, "\t-R header \tPut header at top of message\n" );
  253.             fprintf( stderr, "\t-x file   \tFile to delete if posting successful\n" );
  254.             fprintf( stderr, "\t-h        \tFor this help\n\n" );
  255.             exit(0);;
  256.                        break;
  257.  
  258.         default :
  259.  
  260.             fprintf( stderr, " Unknown option use -h for help\n" );
  261.             exit(5);
  262.             break;
  263.  
  264.         }   /* end switch */
  265.  
  266.  
  267.         } else {   
  268.  
  269.         body_fname = argv[i];
  270.         i++;
  271.  
  272.     }   /* end if */
  273.  
  274.   }   /* end while */                    
  275.  
  276. /*
  277.  *=================================================================================================*
  278.  */
  279.  
  280.  
  281. /*
  282.  *   Lock directory while using.
  283.  */
  284.       
  285.   if ( lock = Lock( spooldir, ACCESS_READ ) ) {
  286.  
  287.  
  288. /*
  289.  *   Open file to spool articles to.
  290.  */
  291.  
  292.       sprintf( Buffer, "%s/msg.%ld", spooldir, time( NULL ) );
  293.       if ( msg_fptr = fopen( Buffer, "w" ) ) {
  294.  
  295. /*
  296.  *   Add a from line if requested.
  297.  */
  298.  
  299.         if ( from != NULL ) fprintf( msg_fptr, "From: %s\n", from );
  300.  
  301. /*
  302.  *  Open header file and copy to destination if user
  303.  *  has used -R option.
  304.  */
  305.  
  306.         if ( head_fname != NULL ) {
  307.  
  308.             if ( head_fptr = fopen( head_fname, "r" ) ) {
  309.  
  310.                 copyfile( head_fptr, msg_fptr );
  311.                 fclose( head_fptr );
  312.  
  313.             } else {
  314.  
  315.                 fprintf( stderr, " Could not open header file - %s\n", head_fname );
  316.                 exit_code = 10;
  317.     
  318.             }   /* end if */
  319.  
  320.         }   /* end if */
  321.  
  322.         if ( !exit_code ) {
  323.  
  324. /*
  325.  *   Open article/body file or set to stdin if no name
  326.  *   has been specified.
  327.  */
  328.  
  329.             if ( body_fname != NULL ) {
  330.         
  331.                 if ( ( body_fptr = fopen( body_fname, "r" ) ) == NULL ) {
  332.  
  333.                     fprintf( stderr, " Could not open body/article file - %s\n", body_fname );
  334.                     exit_code = 10;
  335.  
  336.                 }   /* end if */
  337.  
  338.             } else {
  339.  
  340.                 body_fptr = stdin;
  341.  
  342.             }   /* end if */
  343.  
  344. /*
  345.  *   All files opened correctly so copy article to destination
  346.  *   file.
  347.  */
  348.  
  349.             if ( !exit_code ) {
  350.  
  351.                 copyfile( body_fptr, msg_fptr );
  352.                 if ( body_fname != NULL ) fclose( body_fptr );
  353.  
  354.  
  355. /*
  356.  *   Successful posting so delete the specified
  357.  *   files
  358.  */
  359.  
  360.                 for ( i = 0; i < num_del; i++ ) {
  361.  
  362.                     status = remove( Todelete[i] );
  363.                     if ( status ) fprintf( stderr, " Failed to delete file - %s\n", Todelete[i] );
  364.  
  365.                 }   /* end for */
  366.  
  367.             }   /* end if */
  368.  
  369.         }   /* end if */
  370.                   
  371.         fclose( msg_fptr );
  372.         if ( exit_code ) remove( Buffer );
  373.  
  374.     } else {
  375.  
  376.         fprintf( stderr, " Could not open message file - %s\n", Buffer );
  377.         exit_code = 10;
  378.  
  379.     }   /* end if open msg file */
  380.  
  381.     UnLock( lock );
  382.  
  383.   } else {
  384.  
  385.     fprintf( stderr, " Could not lock spooldir - %s\n", spooldir );
  386.     exit_code = 20;
  387.  
  388.   }   /* end if lock */
  389.  
  390.     
  391. /*
  392.  *   Check if online. If yes then call sendnews to send off articles
  393.  *   now.
  394.  */
  395.  
  396.   if ( !exit_code && !no_post ) {
  397.  
  398.     if ( ( online = getenv( "ONLINE" ) ) == NULL ) {
  399.         fprintf( stderr, " Please set ONLINE env variable\n" );
  400.         fprintf( stderr, " You are not online - message spooled\n" );
  401.       } else {
  402.         if ( !(con_status = atoi( online )) ) {
  403.             fprintf( stderr, " You are not online - message spooled\n" );
  404.         } else {
  405.             fprintf( stderr, " Posting spooled news now....\n" );
  406.             system("sendnews");
  407.         }  /* end if */
  408.       }   /* end if */
  409.  
  410.   }   /* end if !exit_code */
  411.  
  412.   exit( exit_code );
  413.  
  414. }   /* end program postnews */
  415.  
  416.  
  417. /*========================================================================*
  418.                           BEGIN FUNCTION NextArg
  419.  *========================================================================*/
  420.  
  421. int NextArg( int i, int argc, char *argv[] )
  422.  
  423. {
  424.  
  425.   if ( (i+1) < argc ) {
  426.  
  427.     if ( argv[i+1][0] != '-' ) return TRUE;
  428.  
  429.   }   /* end if */
  430.  
  431.   fprintf( stderr, " Error - No parameter specified following %s\n", argv[i] );
  432.   return FALSE;
  433.  
  434. }   /* end function NextArg */
  435.  
  436.  
  437.  
  438. /*========================================================================*
  439.                           BEGIN FUNCTION copyfile
  440.  *========================================================================*/
  441.  
  442. void copyfile( FILE *in_fptr, FILE *out_fptr )
  443.  
  444.                
  445.   int ch;
  446.  
  447.   while ( ( ch = fgetc( in_fptr ) ) != EOF ) fputc( ch, out_fptr );
  448.  
  449. }   /* end function copyfile */
  450.  
  451. /*========================================================================*
  452.                                    END 
  453.  *========================================================================*/
  454.